package com.iplant.aps.serviceimpl;

import com.iplant.aps.service.LFSService;
import com.iplant.aps.service.po.APIResult;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.lfs.LFSOperationLog;
import com.iplant.aps.service.po.mbs.MBSApiLogAnalysis;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RemoteInvokeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author PengYouWang
 * 2019年12月27日12:44:48
 */

@Service
public class LFSServiceImpl implements LFSService {
    private static final Logger logger = LoggerFactory.getLogger(LFSServiceImpl.class);

    public LFSServiceImpl() {
    }

    private static LFSService Instance;

    public static LFSService getInstance() {
        if (Instance == null)
            Instance = new LFSServiceImpl();
        return Instance;
    }

    @Override
    public APIResult LFS_QueryWorkAreaStationList(BMSEmployee wLoginUser) {
        APIResult wResult = new APIResult();
        try {

            Map<String, Object> wParms = new HashMap<>();
            wParms.put("ID", -1);
            wParms.put("WorkAreaID", -1);
            wParms.put("StationID", -1);
            wParms.put("Active", 1);

            String wUri = StringUtils.Format("api/LFS/WorkAreaAll?cadv_ao={0}&cade_po={1}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public APIResult LFS_QueryAreaDepartmentList(BMSEmployee wLoginUser) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("ID", -1);
            wParms.put("WorkAreaID", -1);
            wParms.put("DepartmentID", -1);
            wParms.put("Active", 1);

            String wUri = StringUtils.Format("api/LFSAreaDepartment/All?cadv_ao={0}&cade_po={1}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public APIResult LFS_QueryWorkAreaCheckerList(BMSEmployee wLoginUser) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("ID", -1);
            wParms.put("WorkAreaID", -1);
            wParms.put("CheckerID", -1);
            wParms.put("Active", 1);

            String wUri = StringUtils.Format("api/LFSWorkAreaChecker/All?cadv_ao={0}&cade_po={1}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult LFS_QueryAreaDepartment(BMSEmployee wLoginUser, int wID, String wName) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("ID", wID);
            wParms.put("Name", wName);

            String wUri = StringUtils.Format("api/LFSAreaDepartment/Info?cadv_ao={0}&cade_po={1}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult LFS_QueryStoreHouseList(BMSEmployee wLoginUser) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("ID", -1);
            wParms.put("Active", -1);

            String wUri = StringUtils.Format("api/LFS/StoreHouseAll?cadv_ao={0}&cade_po={1}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult MBS_UpdateApiLogAnalysis(BMSEmployee wLoginUser, MBSApiLogAnalysis wMBSApiLogAnalysis) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("data", wMBSApiLogAnalysis);

            String wUri = StringUtils.Format("api/MBSApiLogAnalysis/Update?cadv_ao={0}&cade_po={1}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.POST);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult BMS_QueryChecker(BMSEmployee wLoginUser, int wProductID, int wStationID, int wClassID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("ProductID", wProductID);
            wParms.put("StationID", wStationID);
            wParms.put("ClassID", wClassID);

            String wUri = StringUtils.Format("api/BMSWorkChargeItem/QueryChecker?cadv_ao={0}&cade_po={1}&company_id" +
                            "={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult BMS_QueryDeliveryClerk(BMSEmployee wLoginUser, int wProductID, int wStationID, int wClassID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("ProductID", wProductID);
            wParms.put("StationID", wStationID);
            wParms.put("ClassID", wClassID);

            String wUri = StringUtils.Format("api/BMSWorkChargeItem/BMS_QueryDeliveryClerk?cadv_ao={0}&cade_po={1" +
                            "}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult BMS_QueryCraftsman(BMSEmployee wLoginUser, int wProductID, int wStationID, int wClassID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("ProductID", wProductID);
            wParms.put("StationID", wStationID);
            wParms.put("ClassID", wClassID);

            String wUri = StringUtils.Format("api/BMSWorkChargeItem/QueryCraftsman?cadv_ao={0}&cade_po={1}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult WMS_CreateNewDemandNo(BMSEmployee wLoginUser) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            String wUri = StringUtils.Format("api/WMSDemandNo/CreateNewDemandNo?cadv_ao={0}&cade_po={1}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult WMS_CreateNewDemandNoOut(BMSEmployee wLoginUser) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            String wUri = StringUtils.Format("api/WMSDemandNo/CreateNewDemandNoOut?cadv_ao={0}&cade_po={1}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult LFS_UpdateOperationLog(BMSEmployee wLoginUser, LFSOperationLog wLFSOperationLog) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("data", wLFSOperationLog);

            String wUri = StringUtils.Format("api/LFS/UpdateOperationLog?cadv_ao={0}&cade_po={1}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.POST);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult BMS_QueryWorkChargeStepList(BMSEmployee wLoginUser, int wWorkChargeID, int wClassID, int wStationID, int wProductID, int wPartPointID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("WorkChargeID", wWorkChargeID);
            wParms.put("ClassID", wClassID);
            wParms.put("StationID", wStationID);
            wParms.put("ProductID", -1);
            wParms.put("PartPointID", wPartPointID);

            String wUri = StringUtils.Format("api/BMSWorkChargeStep/All?cadv_ao={0}&cade_po={1}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }
}
